<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tools @addDepts="addDepts" :treeNodes="company" :isRoot="true"></tree-tools>
        <el-tree :data="departs" :props="defaultsProps" :default-expand-all="false">
         <tree-tools slot-scope="{ data }" :treeNodes="data" @delDepts="getDepartments" @editDepts="editDepts" @addDepts="addDepts"></tree-tools>
        </el-tree>
      </el-card>
      <add-depts ref="addDept" :showDialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments"/>
    </div>
  </div>
</template>

<script>
import TreeTools  from './components/tree-tools'
import AddDepts  from './components/add-depts'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/auth'
export default {
  components: {
    TreeTools,
    AddDepts
  },
  data() {
    return {
      showDialog: false,
      company: { },
       departs: [{
         name: '总裁办',
         manager: '曹操',
         children: [{
           name: '董事会',
           manager: '刘备',
         }]},
         {
           name: '行政部',
           manager: '曹丕',
         },
         {
           name: '人事部',
           manager: '曹德',
         }],
      defaultsProps: {
        label: 'name'
      },
      node: null
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments () {
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: ''}
     this.departs = tranListToTreeData(result.depts, '')
     },
     addDepts(node) {
       this.showDialog = true
       this.node = node
     },
     editDepts (node) {
        this.showDialog = true
       this.node = node
       this.$refs.addDept.getDepartDetail(node.id)
     }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
